//
//  HMPayOrderViewController.m
//  HMMarket
//
//  Created by maimaigo3 on 2017/5/18.
//  Copyright © 2017年 homai. All rights reserved.
//

#import "HMPayOrderViewController.h"
#import "HMPayOrderTableViewCell.h"
#import "HMProPayInfoTableViewCell.h"
#import "HMOrderList.h"
@interface HMPayOrderViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *myTableView;
@property (weak, nonatomic) IBOutlet UIButton *payBtn;
@property (weak, nonatomic) IBOutlet UILabel *orderPriceLabel;
@property (weak, nonatomic) IBOutlet UILabel *surePayOrderLabel;
@property (weak, nonatomic) IBOutlet UIView *backLabel;
@property (weak, nonatomic) IBOutlet UILabel *numberOfOrderLabel;
NS_STRONG HMOrderList *orderList;
@end

@implementation HMPayOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"订单详情";
    [self setSubView];
}

- (void)setSubView {
    _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, KWidthSCREEN, KHeightSCREEN - 48 - 64) style:UITableViewStyleGrouped];
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    [self.view addSubview:_myTableView];
    
    MyLog(@"WAIT_RECEIVE = %d WAIT_RECEIVE_POST = %d", _orederType, _post_status);
    if (_orederType == WAIT_RECEIVE && _post_status == WAIT_RECEIVE_POST) {
//        全部订单,pay_status=0 and post_status=1,待收货,pay_status=0 and post_status=2，为待付款，pay_status=1 and post_status=2已完成
    } else {
//        _payBtn.hidden = YES;

//        _payBtn.frame = CGRectZero;
    }
    
    [_payBtn setBackgroundColor:kMainColor];
    [_payBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_payBtn addTarget:self action:@selector(postSureOrder) forControlEvents:UIControlEventTouchUpInside];
    [self getData];
}

- (void)postSureOrder {
    [HttpBaseTool post:KsureOrder params:@{@"uid":[MGAccountTool getUid], @"oid":_oid} success:^(id responseObj) {
        if ([responseObj[@"code"] integerValue] == 200) {
            [HMHUDManager showHUDWithText:@"确认成功"];
            _refresh(@"nothing");
            LoginSuccess;
            OrderRefresh;
            
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            [HMHUDManager showHUDWithText:responseObj[@"msg"]];
        }
    } failure:^(NSError *error) {
        
    }];
}

- (void)getData {
    [HttpBaseTool get:kConfirmOrder params:@{@"uid":[MGAccountTool getUid], @"oid":_oid} success:^(id responseObj) {
        _orderList = [HMOrderList mj_objectWithKeyValues:responseObj[@"data"]];
        _orderPriceLabel.text = [NSString stringWithFormat:@"订单金额:¥%@", _orderList.total_price] ;
        _surePayOrderLabel.text = [NSString stringWithFormat:@"应付金额:¥%@", _orderList.true_total_price] ;
        _numberOfOrderLabel.text = [NSString stringWithFormat:@"数量:%@", _orderList.count];
        [self.myTableView reloadData];
        
        
        if (_orederType == WAIT_RECEIVE && _post_status == WAIT_RECEIVE_POST) {
            //        全部订单,pay_status=0 and post_status=1,待收货,pay_status=0 and post_status=2，为待付款，pay_status=1 and post_status=2已完成
        } else {
            _payBtn.hidden = YES;
            CGRect orderRect = _orderPriceLabel.frame;
            _orderPriceLabel.frame = CGRectMake(KWidthSCREEN - orderRect.size.width, orderRect.origin.y, orderRect.size.width, orderRect.size.height);
            
            CGRect surePayOrderRect = _surePayOrderLabel.frame;
            _surePayOrderLabel.frame = CGRectMake(KWidthSCREEN - surePayOrderRect.size.width, surePayOrderRect.origin.y, surePayOrderRect.size.width, surePayOrderRect.size.height);
        }

           } failure:^(NSError *error) {
        
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else {
        return _orderList.list.count;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        HMProPayInfoTableViewCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"HMProPayInfoTableViewCell" owner:self options:nil] firstObject];
        return cell;
    } else {
        HMPayOrderTableViewCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"HMPayOrderTableViewCell" owner:self options:nil] firstObject];
        cell.orderDetail = _orderList.list[indexPath.row];
        
        return cell;
    }
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 44;
    } else {
        return 95;
    }
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KWidthSCREEN, 30)];
        
        UILabel *orderNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 300, 30)];
        orderNumberLabel.text = [NSString stringWithFormat:@"订单号：%@", _oid];
        orderNumberLabel.font = [UIFont systemFontOfSize:13.0f];
        orderNumberLabel.textColor = k959595;
        
        UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(KWidthSCREEN - 200, 0, 200, 30)];
        timeLabel.text = _orderList.time;
        timeLabel.textAlignment = NSTextAlignmentRight;
        timeLabel.font = [UIFont systemFontOfSize:13.0f];
        timeLabel.textColor = k959595;
        
        [header addSubview:orderNumberLabel];
        
        [header addSubview:timeLabel];
        
        return header;
    } else {
        return nil;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 30;
    } else {
        return 0.001;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}

-(void)viewDidLayoutSubviews
{
    if ([self.myTableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.myTableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    
    if ([self.myTableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.myTableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
}

@end
